package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    /**
     * 查询敏感词列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(SensitiveDto dto) {
        //1.参数检测
        if(dto==null){
            //如果查询条件为null  参数错误就返回错误信息:  无效的参数
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //如果没有分页条件设置分页条件
        dto.checkParam();
        //2.按名称模糊分页查询
        LambdaQueryWrapper<AdSensitive> queryWrapper=new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(dto.getName())){
            //如果名字不为空 按名字查询一下
            queryWrapper.like(AdSensitive::getSensitives,dto.getName());
        }

        //分页
        IPage<AdSensitive> pageParam=new Page<>(dto.getPage(),dto.getSize());
        IPage<AdSensitive> pageResult = page(pageParam, queryWrapper);

        ResponseResult responseResult=new PageResponseResult(dto.getPage(),dto.getSize(),(int)pageResult.getTotal(),pageResult.getRecords());
        //3.结果封装
        return responseResult;
    }

    /**
     * 新增
     * @param adSensitive
     * @return
     */
    @Override
    @Transactional
    public ResponseResult insert(AdSensitive adSensitive) {
        //1.参数校验  为空或者名字为空都不能新添加
        if(adSensitive==null||StringUtils.isBlank(adSensitive.getSensitives())){
            //非法参数异常
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaQueryWrapper<AdSensitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdSensitive::getSensitives,adSensitive.getSensitives());
        AdSensitive one= getOne(queryWrapper);
        if (!one.getId().equals(adSensitive.getId())){
            return ResponseResult.errorResult(AppHttpCodeEnum.IS_CONTAIN_SAME_NAME);
        }
        //2.逻辑新增
        save(adSensitive);
        //3.结果封装
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改
     *
     * @param adSensitive
     * @return
     */
    @Override
    @Transactional
    public ResponseResult update(AdSensitive adSensitive) {
        //1.参数校验
        if(adSensitive==null||StringUtils.isBlank(adSensitive.getSensitives())){
            //非法参数异常
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询当前的数据是否包含
        AdSensitive adSensitiveById= getById(adSensitive.getId());
        if(adSensitiveById==null){//无效参数异常
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaQueryWrapper<AdSensitive> queryWrapper = new LambdaQueryWrapper<AdSensitive>();
        queryWrapper.eq(AdSensitive::getSensitives,adSensitive.getSensitives());
        if(getOne(queryWrapper)!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //2.逻辑修改
        updateById(adSensitive);
        //3.结果封装
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {
        //1.参数校验
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        AdSensitive one = getById(id);
        if(one==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //2.逻辑删除
        removeById(id);
        //3.结果封装
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
